<<<<<<< HEAD
package com.example.zhangfeng.commondemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhangfeng.commondemo.R;
import com.example.zhangfeng.commondemo.adapter.HomeAdapter;
import com.example.zhangfeng.commondemo.common.L;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangfeng on 2016/4/29.
 * RecyclerView
 */
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private HomeAdapter adapter;
    private List<String> list = new ArrayList<>();

    private MyHandler handler;
    private int lastVisibleItem;
    private LinearLayoutManager mLayoutManager;
    private boolean isRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        L.e("HomeFragment", "onActivityCreated");

        initViews();

        addListeners();
    }

    private void addListeners() {
        swipeRefreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount()) {
                    // 此处进行上拉加载更多
                    swipeRefreshLayout.setRefreshing(true);
                    isRefresh = false;
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void initViews() {

        handler = new MyHandler(this);

        //设置刷新时动画的颜色，可以设置4个
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
        // 这句话是为了，第一次进入页面的时候显示加载进度条
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new HomeAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);

        swipeRefreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        L.e("HomeFragment", "onRefresh");
        isRefresh = true;
        getData();
    }

    private void getData() {
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    static class MyHandler extends Handler {
        WeakReference<HomeFragment> mActivityReference;

        MyHandler(HomeFragment fragment) {
            mActivityReference = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            final HomeFragment fragment = mActivityReference.get();
            if (null != fragment) {
                L.e("HomeFragment", "msg:" + msg.what);
                fragment.swipeRefreshLayout.setRefreshing(false);
                List<String> strList = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    strList.add("" + (int) (Math.random() * 100));
                }
                if (fragment.isRefresh) {
                    fragment.adapter.refreshDataList(strList);
                } else {
                    fragment.adapter.addMoreDataList(strList);
                }
            }
        }
    }

}
=======
package com.example.zhangfeng.commondemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhangfeng.commondemo.R;
import com.example.zhangfeng.commondemo.adapter.HomeAdapter;
import com.example.zhangfeng.commondemo.common.L;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangfeng on 2016/4/29.
 * RecyclerView
 */
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private HomeAdapter adapter;
    private List<String> list = new ArrayList<>();

    private MyHandler handler;
    private int lastVisibleItem;
    private LinearLayoutManager mLayoutManager;
    private boolean isRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        L.e("HomeFragment", "onActivityCreated");

        initViews();

        addListeners();
    }

    private void addListeners() {
        swipeRefreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount()) {
                    // 此处进行上拉加载更多
                    swipeRefreshLayout.setRefreshing(true);
                    isRefresh = false;
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void initViews() {

        handler = new MyHandler(this);

        //设置刷新时动画的颜色，可以设置4个
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
        // 这句话是为了，第一次进入页面的时候显示加载进度条
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new HomeAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);

        swipeRefreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        L.e("HomeFragment", "onRefresh");
        isRefresh = true;
        getData();
    }

    private void getData() {
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    static class MyHandler extends Handler {
        WeakReference<HomeFragment> mActivityReference;

        MyHandler(HomeFragment fragment) {
            mActivityReference = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            final HomeFragment fragment = mActivityReference.get();
            if (null != fragment) {
                L.e("HomeFragment", "msg:" + msg.what);
                fragment.swipeRefreshLayout.setRefreshing(false);
                List<String> strList = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    strList.add("" + (int) (Math.random() * 100));
                }
                if (fragment.isRefresh) {
                    fragment.adapter.refreshDataList(strList);
                } else {
                    fragment.adapter.addMoreDataList(strList);
                }
            }
        }
    }

}
>>>>>>> 12b9a08f9380bce72b8c6e055d4084faa0570803
